package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.DishService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mwj
 * @date 2021/11/25
 * @TIME:15:01
 */
@Slf4j
@RequestMapping("/dish")
@RestController
@Api(description = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;

    @PostMapping
    @CacheEvict(value = "DISH_CACHE",key="'DISH'+#dishDto.categoryId+'_'+#dishDto.status")
    public R addDish(@RequestBody DishDto dishDto) {
        log.info("dishDto = " + dishDto);
        dishService.addDish(dishDto);
        return R.success("添加成功");

    }

    @GetMapping("/page")
    public R findByPage(Integer page, Integer pageSize, String name) {
        Page<DishDto> ipage = dishService.findByPage(page, pageSize, name);
        return R.success(ipage);
    }

    @GetMapping("/{id}")//restful风格
    public R findById(@PathVariable Long id) {
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    @PutMapping
    @CacheEvict(value = "DISH_CACHE",allEntries = true)
    public R update(@RequestBody DishDto dishDto){

        dishService.updateDishDto(dishDto);
        return R.success("修改成功");

    }
    //根据分类查询菜品列表
    //优化 在手机端还需返回口味列表
    @Cacheable(value = "DISH_CACHE",key = "'DISH'+#categoryId+'_'+#status")
    @GetMapping("/list")
    public R findDishesByCategoryId(Long categoryId,Integer status){
      List<DishDto> disheDtos= dishService.findDishesByCategoryId(categoryId,status);

        return R.success(disheDtos);
    }

    //菜品管理---批量删除
    //需逻辑删除菜品表，菜品口味，如果是套餐内拒绝删除
    @DeleteMapping
    @CacheEvict(value = "DISH_CACHE",allEntries = true)
    public R deleteByIds(Long[] ids){
        return  dishService.deleteByIds(ids);

    }


    //菜品管理---批量启售/批量停售
    @PostMapping("/status/{statusHandle}")
    public R statusByIds(@PathVariable Integer statusHandle, Long[] ids){
        dishService.statusByIds(statusHandle,ids);
        return R.success("操作成功");
    }

}
